<?php


class GrabController extends BaseController {

	public function debug()
	{
		$keyword = Input::get('key');
		$limit = Input::get('limit');
		$imgtype = Input::get('type');
		$imgsz = Input::get('size');
		
		echo $keyword.$limit.$imgtype.$imgsz;
	}

	public function searchimage()
	{
		$page = Input::get('page');
		$keyword = Input::get('key');
		$limit = Input::get('limit');
		$imgtype = Input::get('type');
		$imgsz = Input::get('size');
		$as_filetype = Input::get('filetype');
		$as_sitesearch = Input::get('fromsite');
		$limit = 8;
		$keyword = str_replace(' ', '%20', $keyword);
		$data=$this->getimage($page,$keyword,$limit, $imgtype, $imgsz, $as_filetype,$as_sitesearch);
		echo json_encode($data);
	}

	public function getimage($page,$keyword,$limit, $imgtype='', $imgsz='', $as_filetype='',$as_sitesearch='')
	{
		
		$imgtype=($imgtype!='') ? '&imgtype='.$imgtype : '' ;
		$imgsz=($imgsz!='') ? '&imgsz='.$imgsz : '' ;
		$as_filetype=($as_filetype!='') ? '&as_filetype='.$as_filetype : '' ;
		$as_sitesearch=($as_sitesearch!='') ? '&as_sitesearch='.$as_sitesearch : '' ;
		$rpage=($page*8)-8;
		/*
		 imgsz=icon restricts results to small images
		imgsz=small|medium|large|xlarge restricts results to medium-sized images
		imgsz=xxlarge restricts results to large images
		imgsz=huge restricts resykts to extra-large images
		 */
		
		$k=0;
		do {
			$url='https://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&start='.$rpage.'&q='.$keyword.$imgtype.$imgsz.$as_filetype.$as_sitesearch;
			$json = file_get_contents($url);
			$json_res = json_decode($json, TRUE);
			foreach ($json_res['responseData']['results'] as $key => $res) {
				//echo '<a href="' . $res['url'] . '">' . $res['url'] . '</a>' . '</br>';
				$images[$k]['url']=$res['url'];
				$images[$k]['tbUrl']=$res['tbUrl'];
				$images[$k]['width']=$res['width'];
				$images[$k]['height']=$res['height'];
				$delta = 24;
				$reduce_brightness = true;
				$reduce_gradients = true;
				$num_results = 5;
				$k++;
				if($k>=$limit) break;
			}
		} while ($k<$limit);
		
		return $images;
	}
	
	public function saveimage()
	{
		$url = Input::get('url');
		$filename_from_url = parse_url($url);
		$ext = pathinfo($filename_from_url['path'], PATHINFO_EXTENSION);
		$width = Input::get('width');
		$height = Input::get('height');
		$name = str_replace(' ', '-', Input::get('name')).'.'.$ext;
		$result= 'success">Success '.$name;
		if($this->putimg($url,$name))
		{
			$data['user_id']='1';
			$data['category_id']='';
			$data['height']=$height;
			$data['width']=$width;
			$data['source']=$url;
			$data['name']=$name;
			$data['date']='';
			$data['text']='test description';
			
			//insert
		}else
		{
			$result= 'important">Error '.$name;
		}
		echo '<span class="label label-'.$result.'</span></br>';
	}
	
	public function putimg($url='', $name='')
	{
		
		$url = $url;
		$img = 'storage/'.$name;
		//file_put_contents($img, file_get_contents($url));
		if(copy($url, $img))
		{
			return true;
		}else return false;
	}
	
	public function gimgapi()
	{
		$json = $this->get_url_contents('http://ajax.googleapis.com/ajax/services/search/images?v=1.0&q=sausages');
		
		$data = json_decode($json);
		
		foreach ($data->responseData->results as $result) {
		    $results[] = array('url' => $result->url, 'alt' => $result->title);
		}
		echo '<pre>';
		print_r($data);
		echo '</pre>';
	}
	
	public function get_url_contents($url) {
	    $crl = curl_init();
	
	    curl_setopt($crl, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
	    curl_setopt($crl, CURLOPT_URL, $url);
	    curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
	    curl_setopt($crl, CURLOPT_CONNECTTIMEOUT, 5);
	
	    $ret = curl_exec($crl);
	    curl_close($crl);
	    return $ret;
	}
	
}